import Vue from 'vue'
import Vuex from 'vuex'
import http from "@/util/http";
import createPersistedState from "vuex-persistedstate";
Vue.use(Vuex)

// const store = new Vuex.Store({
//   // ...
//   plugins: [createPersistedState({
//     storage:window.sessionStorage,    //默认localstorage
//     reducer:(state) =>{
//       return{
//         user:state.userCookie   //只存储state中的userCookie
//       }//默认全部存储
//     }
//   })],
// });

export default new Vuex.Store({
  state: {
    cityId:'1',
    cityName:'南宁',
    cinemaList:[],
    userCookie:''
  },
  getters: {
  },
  mutations: {
    changeCityName (state,cityName){
      state.cityName = cityName
    },
    changeCinemaData (state, data){
      state.cinemaList = data
    },
    changeCookie (state,data){
      state.userCookie = data
    },
    emptyCookie (state,data){
      state.userCookie = ''
    }
  },
  actions: {
    getCinemaData (store){
      http(`posts${store.state.cityId}`).then(res =>{
        // this.list = [...this.list,...res.data]
        store.commit("changeCinemaData", res.data)
      })
    }
  },
  modules: {
  },
  plugins: [
    // 把vuex的数据存储到sessionStorage
    createPersistedState({
      storage: window.sessionStorage,
      reducer:(state) =>{
        return {
          // 只存储state中的userCookie
          userCookie: state.userCookie
        }
      }
    }),
  ],
})
